package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.*;

/**
 * 预约设置实现类
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    /**
     * 预约设置
     * @param settings
     */
    @Override
    public void setAndUpdate(List<String[]> settings) {
        // 将数组转成OrderSetting对象
        OrderSetting orderSetting = new OrderSetting();
        // 已预约人数默认为0
        orderSetting.setReservations(0);
        if (settings != null && settings.size() > 0) {

            for (String[] setting : settings) {
                // 设置可预约日期
                orderSetting.setOrderDate(new Date(setting[0]));
                // 设置可预约人数
                orderSetting.setNumber(Integer.parseInt(setting[1]));

                // 判断当前日期是否已经设置预约数据
                int order = orderSettingDao.count(orderSetting);
                if (order > 0) {
                    // 如果已经设置了预约人数,则进行预约数据更新
                    orderSettingDao.updateOrder(orderSetting);
                    // 跳过本次循环,不再执行setOrder()
                    continue;
                }
                // 否则添加预约设置
                orderSettingDao.setOrder(orderSetting);

            }
        }


    }

    /**
     * 导出预约设置
     * @return
     */
    @Override
    public List<OrderSetting> downLoadSetting() {
        return orderSettingDao.findAll();
    }

    /**
     * 按月查询预约设置
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSetting(String date) {
        List<OrderSetting> list = orderSettingDao.getOrderSetting(date);
        // 创建一个map的集合
        ArrayList<Map> maplist = new ArrayList<>();
        if (list != null && list.size() > 0) {
            // 集合不为空时遍历集合
            for (OrderSetting orderSetting : list) {
                HashMap<String, Object> map = new HashMap<>();
                // 把日期放入map
                map.put("date",orderSetting.getOrderDate().getDate());
                // 放入可预约人数
                map.put("number",orderSetting.getNumber());
                // 放入已预提人数
                map.put("reservations",orderSetting.getReservations());
                maplist.add(map);
            }
        }
        return maplist;
    }

    /**
     * 根据日期修改预约人数
     * @param orderSetting
     */
    @Override
    public void updateOrderSetting(OrderSetting orderSetting) {
        // 判断当前日期是否已经设置预约数据
        int order = orderSettingDao.count(orderSetting);
        if (order > 0) {
            // 如果已经设置了预约人数,则进行预约数据更新
            orderSettingDao.updateOrder(orderSetting);
        } else {
            // 否则添加预约设置
            orderSettingDao.setOrder(orderSetting);

        }
    }
}
